// poll  将输入和输出事件进行分离
//  参数    
/* 
    pollfd fds  
        包含 三个参数的结构体数组
            fd events 用户 -> 内核 revent 内核->用户
    nfds_t nfds 数组个数
    int timeout 
*/
// 返回值
/*0 超时
    >0 有n个fd就绪
    <0 出错
*/
//缺点
/*
    效率与select相比差异不大
*/
#pragma once

#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include<poll.h>
#include "sockfd.hpp"

using namespace std;

static const uint16_t defaultport = 8080;
static const int fd_num_max = 64;
int defaultfd = -1;
int non_event = 0;

class PollServer
{
private:
    Sock _listen;
    uint16_t _port;
    //struct pollfd* _event_fds;
    struct pollfd _event_fds[fd_num_max];

public:
    PollServer(uint16_t port = defaultport) : _port(port)
    {
        for (int i = 0; i < fd_num_max; i++)
        {
            _event_fds[i].fd = defaultfd;
            _event_fds[i].events = non_event;
            _event_fds[i].revents = non_event;
        }
    }
    bool Init()
    {
        _listen.Socket();
        _listen.Bind(_port);
        _listen.Listen();

        return true;
    }
    void Accept()
    {
        string clientip;
        uint16_t clientport;
        int sock = _listen.Accept(&clientip, &clientport);
        if (sock < 0)
            return;
        lg(Info, "accept success,%s:%d,sock_fd:%d", clientip, clientport, sock);

        // 处理
        int pos = 1;
        for (; pos < fd_num_max; pos++)
        {
            if (_event_fds[pos].fd != defaultfd)
                continue;
            else
                break;
        }
        if (pos == fd_num_max)
        {
            lg(Warning, "server is full,close %d", sock);
            close(sock);
            //可以考虑扩容
        }
        else
        {
            // 将链接添加到辅助数组
            _event_fds[pos].fd = sock;
            _event_fds[pos].events = POLLIN;
            _event_fds[pos].revents = non_event;
        }
    }
    void Recver(int fd,int pos)
    {
        char *buffer;
        ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            cout << "get a message: " << buffer << endl;
        }
        else if (n == 0)
        {
            lg(Info, "client quit,close fd:%d", fd);
            close(fd);
            _event_fds[pos].fd = defaultfd;
        }
        else
        {
            lg(Warning, "read error fd:%d", fd);
            close(fd);
            _event_fds[pos].fd = defaultfd;
        }
    }
    void Dispatcher()//派发
    {
        for (int i = 0; i < fd_num_max; i++)
        {
            int fd = _event_fds[i].fd;
            if (fd == defaultfd)
                continue;

            if (_event_fds[i].revents & POLLIN)
            {
                if (fd == _listen.Fd()) // 或取新链接
                {
                    Accept();
                }
                else
                {
                    //
                    Recver(fd,i);
                }
            }
        }
    }
    void Start()
    {
        _event_fds[0].fd = _listen.Fd();
        _event_fds[0].events = POLLIN;
        int timeout = 3000;//毫秒
        for (;;)
        {
            int n = poll(_event_fds,fd_num_max,timeout);
            switch (n)
            {
            case 0:
                cout << "time out" << endl;
                break;
            case -1:
                cerr << "poll error" << endl;
                break;
            default:
                cout << "get a new link" << endl;
                Dispatcher();
                break;
            }
        }
    }
    ~PollServer()
    {
        _listen.Close();
    }
};